<?php

namespace App\Services;

use Illuminate\Support\Facades\Storage;

use Exception;

class UploaderService
{
    /**
     * 上传文件
     */
    public static function upload($request)
    {
        $params = $request->input();
        if (!empty($params['total_count']) && !empty($params['total_count'])) {
            if (!empty($params['multi']) && $params['multi'] == 'true') {
                if ($params['total_count'] - $params['count'] <= 0) {
                    return response()->json(['code' => 0, 'msg' => '最多上传' . $params['total_count'] . '张']);
                }
            }
        }

        $folder = $request->get('folder');
        !$folder && $folder = 'default';
        $field = $request->get('field');
        !$field && $field = 'file';

        $file = $request->file($field);
        // 验证一下上传文件是否有效：
        if ($file->isValid()) {
            $storage = $request->get('place') ?? 'qiniu';
            if ($storage == 'local') {
                // 上传到本地
                try {
                    // 获取文件相关信息
                    $originalName = $file->getClientOriginalName(); // 文件原名
                    $ext = $file->getClientOriginalExtension(); // 扩展名
                    $realPath = $file->getRealPath(); // 临时文件的绝对路径
                    $type = $file->getClientMimeType(); // image/jpeg
                    //$size = $file->getClientSize(); // 文件的大小

                    $folderName = $folder . '/' . date('Y-m') . '/' . date('d');
                    $filename = $folderName . '/' . uniqid() . '.' . $ext;
                    // 这里的uploads是配置文件的名称
                    $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
                    if ($bool) {
                        return response()->json([
                            'code' => 1,
                            'msg' => '上传成功',
                            'filepath' => $filename,
                            'full_filepath' => asset('uploads/'. $filename),
                            'original_name' => $originalName,
                            'ext' => $ext,
                            'type' => $type
                        ]);
                    } else {
                        return response()->json(['code' => 0, 'msg' => '上传失败']);
                    }
                } catch (Exception $e) {
                    return response()->json(['code' => 0, 'msg' => $e->getMessage() . ' 支持jpg,jpeg,png,gif类型，且最大500KB']);
                }
            } elseif ($storage == 'qiniu') {
                $qiniu = new Qiniu();
                $result = $qiniu->uploadFile($_FILES[$field]);
                if ($result['code'] == 1) {
                    $filepath = config('filesystems.disks.qiniu.url') . $result['key'];

                    return response()->json([
                        'code' => 1,
                        'msg' => '上传成功',
                        'filepath' => $filepath,
                        'full_filepath' => $filepath,
                    ]);
                } else {
                    return response()->json(['code' => -1, 'msg' => $result['msg']]);
                }
            } else {
                return response()->json(['code' => -1, 'msg' => '不支持的上传空间']);
            }
        } else {
            return response()->json(['code' => -1, 'msg' => '无效的文件']);
        }
    }
}
